package com.mxpio.erp.equipment.service.impl;

import cn.hutool.json.JSONUtil;
import com.mxpio.erp.common.equipment.entity.EquipmentArea;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpRelCategory;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpStatusRecords;
import com.mxpio.erp.common.equipment.service.EquipmentAreaService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpBasicsService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpStatusRecordsService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.DictCacheService;
import com.mxpioframework.system.service.DictService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 设备台账Service业务层处理
 *
 * @author Cheng
 * @date 2023-07-01
 */
@Service
@Slf4j
public class EquipmentEqpBasicsServiceImpl extends BaseServiceImpl<EquipmentEqpBasics> implements EquipmentEqpBasicsService {

    @Resource
    private EquipmentAreaService equipmentAreaService;

    @Resource
    private EquipmentEqpStatusRecordsService eqpStatusRecordsService;

    private DictCacheService cacheService;

    private DictService dictService;

    @Override
    @Transactional(readOnly = false)
    public void saveEqp(EquipmentEqpBasics equipmentEqpBasics) {
        EquipmentArea area = JpaUtil.getOne(EquipmentArea.class, equipmentEqpBasics.getAreaId());
        equipmentEqpBasics.setAreaName(equipmentAreaService.getParentAllNameByPid(area.getPid()) + "/" + area.getAreaName());
        equipmentEqpBasics.setIsDel(0);
        this.save(equipmentEqpBasics);

        //设置设备分类中间表
        String eqpTypeId = equipmentEqpBasics.getEqpTypeId();
        List<String> arr = Arrays.asList(eqpTypeId.split(","));
        for (String s : arr) {
            EquipmentEqpRelCategory bean = new EquipmentEqpRelCategory();
            bean.setEqpId(equipmentEqpBasics.getId());
            bean.setCategoryId(s);
            JpaUtil.save(bean);
        }

        //添加设备状态记录
        EquipmentEqpStatusRecords records = new EquipmentEqpStatusRecords();
        records.setEqpId(equipmentEqpBasics.getId());
        records.setIsDel(0);
        records.setNowStatus(equipmentEqpBasics.getStatus());
        records.setReason("添加设备-状态变更");
        JpaUtil.save(records);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateEqpById(EquipmentEqpBasics equipmentEqpBasics) {
        EquipmentArea area = JpaUtil.getOne(EquipmentArea.class, equipmentEqpBasics.getAreaId());
        equipmentEqpBasics.setAreaName(equipmentAreaService.getParentAllNameByPid(area.getPid()) + "/" + area.getAreaName());

        JpaUtil.lind(EquipmentEqpRelCategory.class).equal("eqpId", equipmentEqpBasics.getId()).delete();

        //设置设备分类中间表
        String eqpTypeId = equipmentEqpBasics.getEqpTypeId();
        List<String> arr = Arrays.asList(eqpTypeId.split(","));
        for (String s : arr) {
            EquipmentEqpRelCategory bean = new EquipmentEqpRelCategory();
            bean.setEqpId(equipmentEqpBasics.getId());
            bean.setCategoryId(s);
            JpaUtil.save(bean);
        }
        EquipmentEqpBasics bean = this.getById(EquipmentEqpBasics.class, equipmentEqpBasics.getId());

        if (bean.getStatus() != equipmentEqpBasics.getStatus()) {
            //更新上一条记录的下一个操作时间
            eqpStatusRecordsService.saveRecordAndUpdatePreRecord(bean, equipmentEqpBasics.getStatus(), "修改设备-状态变更");
        }
        this.update(equipmentEqpBasics);
    }

    @Override
    @Transactional
    public void bindNFC(EquipmentEqpBasics equipmentEqpBasics) {
        EquipmentEqpBasics basics = this.getById(EquipmentEqpBasics.class, equipmentEqpBasics.getId());
        if (null == basics) {
            throw new MBootException("未找到对应设备");
        }
        //清空该NFC之前绑定
        JpaUtil.linu(EquipmentEqpBasics.class).set("nfcNumber", null).equal("nfcNumber", equipmentEqpBasics.getNfcNumber()).update();
        basics.setNfcNumber(equipmentEqpBasics.getNfcNumber());
        this.update(basics);
    }

    @Override
    public String bindNFCList(String listStr) {
        List<EquipmentEqpBasics> list = JSONUtil.toList(JSONUtil.parseArray(listStr), EquipmentEqpBasics.class);
        int errorNum= 0;
        ArrayList ids = new ArrayList<>();
        for (EquipmentEqpBasics tpmEqpBasics : list) {
            try{
                if (tpmEqpBasics ==null || StringUtils.isBlank(tpmEqpBasics.getId())){
                    throw new MBootException("上传设备id或者实体不存在！");
                }
                this.bindNFC(tpmEqpBasics);
                ids.add(tpmEqpBasics.getId());
            }catch (Exception e){
                errorNum++;
                log.error("设备ID："+tpmEqpBasics.getId()+"，绑定NFC失败");
                e.printStackTrace();
            }
        }

        if (errorNum>0){
            return  JSONUtil.toJsonStr(ids);
        }
        return null;
    }

    @Override
    @Transactional
    public EquipmentEqpBasics getByCode(String code) {
        return JpaUtil.linq(EquipmentEqpBasics.class).equal("eqpCode", code).findOne();
    }
}
